using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FormInputProject.Models;

namespace FormInputProject.Controllers
{   
    public class RecordController : Controller
    {
		private readonly IPointRepository pointRepository;
		private readonly IRecordRepository recordRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public RecordController() : this(new PointRepository(), new RecordRepository())
        {
        }

        public RecordController(IPointRepository pointRepository, IRecordRepository recordRepository)
        {
			this.pointRepository = pointRepository;
			this.recordRepository = recordRepository;
        }

        //
        // GET: /Record/

        public ViewResult Index()
        {
            return View(recordRepository.AllIncluding(record => record.Point));
        }

        //
        // GET: /Record/Details/5

        public ViewResult Details(int id)
        {
            return View(recordRepository.Find(id));
        }

        //
        // GET: /Record/Create

        public ActionResult Create()
        {
			ViewBag.PossiblePoint = pointRepository.All;
            return View();
        } 

        //
        // POST: /Record/Create

        [HttpPost]
        public ActionResult Create(Record record)
        {
            if (ModelState.IsValid) {
                recordRepository.InsertOrUpdate(record);
                recordRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossiblePoint = pointRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Record/Edit/5
 
        public ActionResult Edit(int id)
        {
			ViewBag.PossiblePoint = pointRepository.All;
             return View(recordRepository.Find(id));
        }

        //
        // POST: /Record/Edit/5

        [HttpPost]
        public ActionResult Edit(Record record)
        {
            if (ModelState.IsValid) {
                recordRepository.InsertOrUpdate(record);
                recordRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossiblePoint = pointRepository.All;
				return View();
			}
        }

        //
        // GET: /Record/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(recordRepository.Find(id));
        }

        //
        // POST: /Record/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            recordRepository.Delete(id);
            recordRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                pointRepository.Dispose();
                recordRepository.Dispose();
            }
            base.Dispose(disposing);
        }

        public ActionResult FormRecordInput()
        {
            if (Session["UserName"] == null || Convert.ToString(Session["UserName"]) == "")
            {
                return RedirectToAction("Index", "Home");
            }

            return View(pointRepository.All);
        }

        [HttpPost]
        public ActionResult FormRecordInput(FormCollection collection)
        {
            if (collection != null && collection.Count > 0)
            {
                foreach (string value in collection)
                {
                    if (collection[value].Trim() != "")
                    {
                        //recordRepository.Find()
                    }
                }
            }

            return View();
        }
    }
}

